package com.study.zookeeper.curator;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;

public class ListenCurator {


    private static String connectStr = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2184";
    private static RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    private static String path= "/zlx";
    private static CuratorFramework client = CuratorFrameworkFactory
            .builder()
            .connectString(connectStr)
            .sessionTimeoutMs(5000)
            .retryPolicy(retryPolicy)
            //指定命名空间
            // .namespace("")
            .build();

    public static void main(String[] args) throws Exception {

        client.start();

        //NodeCache 不仅可以用于监听数据节点的内容变更，也能监听指定节点是否存在，如果节点不存在，在节点创建之后出发NodeCacheListener,
        //如果该节点被删除，那么Curator就无法出发NodeCacheListener, true 不触发
        final NodeCache nodeCache = new NodeCache(client, path, false);
        nodeCache.start();
        nodeCache.getListenable().addListener(new NodeCacheListener() {
            public void nodeChanged() throws Exception {
                System.out.println("Node data update, new data:" + new String(nodeCache.getCurrentData().getData()));
            }
        });

        final PathChildrenCache pathChildrenCache = new PathChildrenCache(client, path, false);
        pathChildrenCache.start();
        pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
            public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
                switch (pathChildrenCacheEvent.getType()){
                    case CHILD_ADDED:
                        System.out.println("CHILD_ADDED: " + pathChildrenCacheEvent.getData());
                        break;
                    case CHILD_UPDATED:
                        System.out.println("CHILD_UPDATE: " + pathChildrenCacheEvent.getData());
                        break;
                    case CHILD_REMOVED:
                        System.out.println("CHILD_REMOVE: " + pathChildrenCacheEvent.getData());
                        break;
                        default:
                            break;

                }

            }
        });
        client.create()
                .creatingParentContainersIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .forPath(path, "init".getBytes());
        System.out.println(new String(client.getData().forPath(path)));
        client.setData().forPath(path, "1234".getBytes());
        System.out.println(new String(client.getData().forPath(path)));
        client.setData().forPath(path, "12345678".getBytes());
        System.out.println(new String(client.getData().forPath(path)));
        Thread.sleep(1000);
        client.create().withMode(CreateMode.EPHEMERAL).forPath(path + "/c1", "c1".getBytes());
        Thread.sleep(1000);
        client.delete().forPath(path + "/c1");
        Thread.sleep(1000);
        client.delete().deletingChildrenIfNeeded().forPath(path);
        Thread.sleep(Integer.MAX_VALUE);

    }
}
